package com.example.booksystem.view;

import com.example.booksystem.controller.UserController;
import com.example.booksystem.model.User;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.sql.SQLException;

public class Login {
    private Stage stage;
    private Scene scene;
    private String[] strings = {"账号:","密码:","请输入账号","请输入密码"};
    private TextField[] textField;
    private UserController controller;
    public Login(Stage stage){
        this.stage = stage;
        controller = new UserController();
    }
    public void show(){
        VBox vBox = new VBox();
        textField = new TextField[2];
        vBox.setAlignment(Pos.CENTER);
        for (int i = 0; i < 2; i++) {
            Label label = new Label(strings[i]);
            textField[i] = new TextField();
            textField[i].setPromptText(strings[i+2]);
            HBox hBox =new HBox();
            hBox.setAlignment(Pos.CENTER);
            hBox.getChildren().addAll(label,textField[i]);
            vBox.getChildren().add(hBox);
        }

        Button[] button = new Button[2];
        button[0] = new Button("登录");
        button[1] = new Button("注册");
        for (int i = 0; i < 2; i++) {
            buttonAction(button);
            HBox hBox = new HBox();
            hBox.setAlignment(Pos.CENTER);
            hBox.getChildren().add(button[i]);
            vBox.getChildren().add(hBox);
        }
        scene = new Scene(vBox,240,150);
        stage.setScene(scene);
        stage.setTitle("图书管理系统");
        stage.show();
    }

    private void buttonAction(Button[] button) {
        button[0].setOnAction(e->{
            String username = textField[0].getText();
            String password = textField[1].getText();
            User user = new User(username,password);
            User status = controller.login(user);
            System.out.println("status=" + status);
            if (status == null){
                showError("账户不存在");
            }else if (status.getUserType() == -1){
                showError("密码错误");
            }else {
                //显示管理员或用户界面;
                Index index = new Index(stage);
                index.show(status);
            }
        });
        button[1].setOnAction(e->{
            String username = textField[0].getText();
            String password = textField[1].getText();
            User user = new User(username,password);
            Integer status = null;
            try {
                status = controller.sign(user);
            } catch (SQLException ex) {
                showError("注册失败,账号已存在");
                ex.printStackTrace();
            }
            System.out.println("status=" + status);
            if (status==1){
                showSuccess("注册成功");
            }
        });
    }

    private void showError(String message) {
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setTitle("Error");
        alert.setHeaderText(null);
        alert.setContentText(message);
        alert.showAndWait();
    }
    private void showSuccess(String message) {
        Stage stage = new Stage();
        VBox vBox = new VBox();
        HBox hBox1 = new HBox();
        HBox hBox2 = new HBox();
        Label label = new Label(message);
        Button exit = new Button("确定");
        exit.setOnAction(e->{
            stage.close();
        });
        hBox1.setAlignment(Pos.CENTER);
        hBox2.setAlignment(Pos.BASELINE_RIGHT);
        hBox1.getChildren().add(label);
        hBox2.getChildren().add(exit);
        vBox.getChildren().addAll(hBox1,hBox2);
        Scene scene = new Scene(vBox,300,100);
        stage.setTitle("success");
        stage.setScene(scene);
        stage.show();
    }
}
